// //存储本地数据
// var searchValue = location.search;
// console.log(searchValue);
// var tarValue = searchValue.slice(1);
// var username = [tarValue.split("&")[0].split("=")[0], tarValue.split("&")[0].split("=")[1]];
// var password = [tarValue.split("&")[3].split("=")[0], tarValue.split("&")[3].split("=")[1]];
// localStorage.setItem(username[0], username[1]);
// localStorage.setItem(password[0], password[1]);

// 生成随机验证码开始
/**
 * 
 * @param {*} canvasId canvas元素的id
 * @returns 
 */
 function code_draw(canvasId) {
    if (!canvasId) return '';
    const $canvasEle = $(`#${canvasId}`)
    let canvas_width = $canvasEle.width();
    let canvas_height = $canvasEle.height();
    let canvas = document.getElementById(canvasId); //获取到canvas的对象，演员
    let context = canvas.getContext("2d"); //获取到canvas画图的环境，演员表演的舞台
    canvas.width = canvas_width;
    canvas.height = canvas_height;
    let sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0"; //取值范围
    let aCode = sCode.split(",");
    let aLength = aCode.length; //获取到数组的长度
    let value = [];
    for (let i = 0; i <= 3; i++) {
        let j = Math.floor(Math.random() * aLength); //获取到随机的索引值
        let deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度
        let txt = aCode[j]; //得到随机的一个内容
        value[i] = txt.toLowerCase();
        let x = 10 + i * 20; //文字在canvas上的x坐标
        let y = 20 + Math.random() * 8; //文字在canvas上的y坐标
        context.font = "bold 23px 微软雅黑";

        context.translate(x, y);
        context.rotate(deg);

        context.fillStyle = code_randomColor();
        context.fillText(txt, 0, 0);

        context.rotate(-deg);
        context.translate(-x, -y);
    }
    // 将生成的值以属性的方法添加到元素
    let valueStr = value.join("");
    $canvasEle.attr('data-code', valueStr)
    //验证码上显示线条
    for (let i = 0; i <= 5; i++) {
        context.strokeStyle = code_randomColor();
        context.beginPath();
        context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
        context.stroke();
    }
    //验证码上显示小点
    for (let i = 0; i <= 30; i++) {
        context.strokeStyle = code_randomColor();
        context.beginPath();
        let x = Math.random() * canvas_width;
        let y = Math.random() * canvas_height;
        context.moveTo(x, y);
        context.lineTo(x + 1, y + 1);
        context.stroke();
    }
    return valueStr;
}
/**
 * 生成随机颜色
 * @returns 颜色值rgb
 */
function code_randomColor() { //得到随机的颜色值
    let r = Math.floor(Math.random() * 256);
    let g = Math.floor(Math.random() * 256);
    let b = Math.floor(Math.random() * 256);
    return "rgb(" + r + "," + g + "," + b + ")";
}

$('#captcha').on('blur', function () {
    checkCode();
})
$(function () {
    code_draw('canvas');
    // 点击后刷新验证码
    $("#canvas").on('click', function () {
        code_draw('canvas');
    })

    // $(".btn").on('click', function () {
    //     checkCode()
    // })
})

function checkCode() {
    // 输入框
    let $inputValueEle = $("#captcha");
    // 获取生成验证码值
    let codeValue = $('#canvas').attr('data-code');
    // 将输入的内容转为大写，可通过这步进行大小写验证
    let inputValue = $inputValueEle.val().toLowerCase();
    if (inputValue == '') {
        // alert('请输入验证码！');
        $('#captcha-error').html(`
        <label  for="captcha-error">请输入验证码</label>
        `);
        return false;
    } else if (inputValue == codeValue) {
        // alert('提交成功！');
        $('#captcha-error').html(`
        <label  for="captcha-error" class="succeed"><i></i></label>
        `);
        return true;
    } else {
        // alert('验证码错误！请重新输入！');
        // $inputValueEle.val('');
        $('#captcha-error').html(`
        <label  for="captcha-error">验证码不正确</label>
        `);
        return false;
    }
}

$('#registsubmit').click(function(){
    if(!checkCode()){
        alert('请正确输入验证码');
        event.stopPropagation();
    }
     else if($('#mobile_phone').val() == '' || $('#pwdRepeat').val()==''){
        alert('请输入账号密码');
        event.stopPropagation();
    }else if($('#mobile_phone').val() == localStorage.getItem('mobile_phone') && $('#pwdRepeat').val()==localStorage.getItem('password')){
        window.event.returnValue=false;
        location.href = "../page/index.html";
    }else{
        alert('请输入正确的账号密码');
    }
    
})


$('.login-switch').click(function(){
    $('.static-form').toggleClass('show');
    $('.quick-form').toggleClass('hide');
})